package com.example.ccj.b.c.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.adapter.LoadImageAdapter;
import com.example.ccj.b.c.recycler.BaseRecycler;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ScreenUtils;
import com.example.ccj.u.SizeUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.media.IPictureSelectResultListener;
import com.example.ccj.u.media.Picture;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 横向多行加载图片控件
 * 根据屏幕尺寸决定每行加载多少个图片
 */
public class TransverseLoadImageControl extends LinearLayout {
    List<LocalMedia> pictureList;
    boolean isCanDelete = true;
    LinearLayout mLlTitle;
    TextView mTvTitleName;
    ImageView mIvTitleImg;
    BaseRecycler<LocalMedia> mRecAddImg;
    SelectPictureMode mode = SelectPictureMode.picture;
    int max = 10;
    LoadImageControlImageListener loadImageControlImageListener;
    LoadImageAdapter loadImageAdapter;

    /**
     *
     * @param context 上下文
     * @param isCanDelete 是否可删除
     * @param max 最大选择数量
     * @param mode 选择模式
     * @param loadImageControlImageListener 选择后的回调
     */
    public TransverseLoadImageControl(Context context,boolean isCanDelete,int max, SelectPictureMode mode,LoadImageControlImageListener loadImageControlImageListener) {
        super(context);
        this.isCanDelete = isCanDelete;
        this.mode = mode;
        this.max = max;
        this.loadImageControlImageListener = loadImageControlImageListener;
        init();
    }

    /**
     *
     * @param context 上下文
     * @param isCanDelete 是否可删除
     * @param mode 选择模式
     * @param loadImageControlImageListener 选择后的回调
     */
    public TransverseLoadImageControl(Context context,boolean isCanDelete, SelectPictureMode mode,LoadImageControlImageListener loadImageControlImageListener) {
        super(context);
        this.isCanDelete = isCanDelete;
        this.mode = mode;
        this.loadImageControlImageListener = loadImageControlImageListener;
        init();
    }

    public TransverseLoadImageControl(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init(){
//        LogUtils.d("初始化000");
        LayoutInflater.from(getContext()).inflate(R.layout.custom_control_ransverse_load_image,this);
        pictureList = new ArrayList<>();

        mLlTitle = findViewById(R.id.custom_control_ransverse_load_image_title);
        mTvTitleName = findViewById(R.id.custom_control_ransverse_load_image_title_name);
        mIvTitleImg = findViewById(R.id.custom_control_ransverse_load_image_title_img);
        mRecAddImg = findViewById(R.id.custom_control_ransverse_load_image_rec);

        //获取屏幕宽度根据宽度设置每行加载的图片数量
        int appScreenWidth = ScreenUtils.getAppScreenWidth();
        int dpWidth = SizeUtils.px2dp(appScreenWidth);
        int i = dpWidth / 70;
//        LogUtils.i("横向加载图片控件-屏幕宽度px:" + appScreenWidth+",屏幕宽度dp:" + dpWidth+",加载数量:" + i);

        mRecAddImg.setLayoutManager(new GridLayoutManager(getContext(), i));
        loadImageAdapter = new LoadImageAdapter(pictureList, isCanDelete, new LoadImageControlImageListener() {
            @Override
            public void onIncrease(List<LocalMedia> pictureList) {
                loadImageControlImageListener.onIncrease(pictureList);
            }

            @Override
            public void onDelete(LocalMedia picture, int position) {
                if (loadImageControlImageListener != null) loadImageControlImageListener.onDelete(picture,position);
            }
        });
        loadImageAdapter.setMode(mode);
        loadImageAdapter.setMax(max);
        mRecAddImg.setAdapter(pictureList,loadImageAdapter);
    }


    /**
     * 显示头部
     * @param name 名称
     * @param imgId 图片id,如果不想设置可设置为0
     */
    public void showTitle(String name,int imgId){
        if (StringUtils.isEmpty(name)) mTvTitleName.setText("图片选择控件");
        else  mTvTitleName.setText(name);
        if (imgId != 0) mIvTitleImg.setImageResource(imgId);
        mLlTitle.setVisibility(VISIBLE);
        mIvTitleImg.setOnClickListener(v-> {
            loadImageAdapter.loadImageMode(new IPictureSelectResultListener() {
                @Override
                public void onResult(ArrayList<LocalMedia> result) {
                    loadImageAdapter.addImage();
                }
            });
        });
    }

    public List<LocalMedia> getPictureList() {
        return pictureList;
    }

    public TransverseLoadImageControl setPictureList(List<LocalMedia> pictureList) {
        mRecAddImg.addDatas(pictureList);
        return this;
    }

    public boolean isCanDelete() {
        return isCanDelete;
    }


    public TransverseLoadImageControl setCanDelete(boolean canDelete) {
        isCanDelete = canDelete;
        if (loadImageAdapter != null) {
            loadImageAdapter.setCanDelete(canDelete);
            loadImageAdapter.notifyDataSetChanged();
        }
        return this;
    }

    public SelectPictureMode getMode() {
        return mode;
    }

    public TransverseLoadImageControl setMode(SelectPictureMode mode) {
        this.mode = mode;
        loadImageAdapter.setMode(mode);
        return this;
    }

    public int getMax() {
        return max;
    }

    public TransverseLoadImageControl setMax(int max) {
        this.max = max;
        loadImageAdapter.setMax(max);
        return this;
    }

    public LoadImageControlImageListener getLoadImageControlImageListener() {
        return loadImageControlImageListener;
    }

    public TransverseLoadImageControl setLoadImageControlImageListener(LoadImageControlImageListener loadImageControlImageListener) {
        this.loadImageControlImageListener = loadImageControlImageListener;
        return this;
    }

    public LinearLayout getmLlTitle() {
        return mLlTitle;
    }

    public TextView getmTvTitleName() {
        return mTvTitleName;
    }
    public ImageView getmIvTitleImg() {
        return mIvTitleImg;
    }

    public BaseRecycler<LocalMedia> getmRecAddImg() {
        return mRecAddImg;
    }

    public LoadImageAdapter getLoadImageAdapter() {
        return loadImageAdapter;
    }

}
